package com.example.travelround.mymusic;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.support.annotation.Nullable;

import java.io.IOException;

/**
 * Created by travelround on 17/3/7.
 */

public class MusicService extends Service {

    private MusicBinder musicBinder = new MusicBinder();
    private CountDownTimer timer;
    private MediaPlayer player = new MediaPlayer();
    private boolean isTimerCount;

    @Override
    public void onCreate() {
        super.onCreate();
        // player = new MediaPlayer();
        timer = new CountDownTimer(Long.MAX_VALUE, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                Intent intent = new Intent("musicBroad");
                int position = player.getCurrentPosition();
                int secTotal = position / 1000;
                int min = secTotal / 60;
                int sec = secTotal % 60;
                intent.putExtra("progress", "" + min + ":" + sec);
                sendBroadcast(intent);
                // Log.d("MusicService", "广播信息");
            }

            @Override
            public void onFinish() {
                isTimerCount = false;
            }
        };


    }

    public void startTimer() {
        if (false == isTimerCount) {
            timer.start();
            isTimerCount = true;
        }
    }

    public void stopTimer() {
        if (true == isTimerCount) {
            timer.cancel();
            isTimerCount = false;
        }
    }


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return musicBinder;
    }

    public class MusicBinder extends Binder {

//        public void play(String urlStr) {
//            player.reset();
//            try {
//                AssetFileDescriptor fileDesc = getAssets().openFd("whzg.mp3");
//                player.setDataSource(fileDesc.getFileDescriptor(), fileDesc.getStartOffset(), fileDesc.getLength());
//
//                fileDesc.close();
//                player.prepare();
//                player.start();
//
////                player.prepareAsync();
////                player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
////                    @Override
////                    public void onPrepared(MediaPlayer mp) {
////                        player.start();
////                    }
////                });
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            startTimer();
//        }

        public void play(String url) {
            player.reset();
            try {
                player.setDataSource(url);
                player.prepareAsync();
                player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        player.start();
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
            // completion();
            startTimer();
        }

    }

}
